/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.manage.service;

import cn.hutool.core.bean.BeanUtil;
import info.peigen.hotpot.business.customer.core.repository.entity.CustomerEntity;
import info.peigen.hotpot.business.customer.manage.convertor.CustomerManageConvertor;
import info.peigen.hotpot.business.customer.manage.form.CustomerManageCreateForm;
import info.peigen.hotpot.business.customer.manage.form.CustomerManageEditForm;
import info.peigen.hotpot.business.customer.manage.form.CustomerManageSearchForm;
import info.peigen.hotpot.business.customer.manage.info.CustomerManageInfo;
import info.peigen.hotpot.business.customer.manage.repository.service.CustomerManageRepository;
import info.peigen.hotpot.common.core.result.PageResult;
import org.beetl.sql.core.query.LambdaQuery;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * <b>(CustomerManageService)</b>
 * 管理后台 "客户" 服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-09-09
 */
@SuppressWarnings("unused")
@Component
public class CustomerManageService {
    @Inject
    CustomerManageRepository customerManageRepository;

    public PageResult<CustomerManageInfo> page(CustomerManageSearchForm form) {
        LambdaQuery<CustomerEntity> lambdaQuery = customerManageRepository.getMapper().createLambdaQuery()
                .andLike(CustomerEntity::getCustomerId, Query.filterLikeEmpty(form.getCustomerId(), false, true))
                .andLike(CustomerEntity::getStatus, Query.filterLikeEmpty(form.getStatus(), false, true))
                .andLike(CustomerEntity::getMobile, Query.filterLikeEmpty(form.getMobile(), false, true))
                .andLike(CustomerEntity::getProxyId, Query.filterLikeEmpty(form.getProxyId(), false, true))
                .andLike(CustomerEntity::getSalesId, Query.filterLikeEmpty(form.getSalesId(), false, true))
                .andLike(CustomerEntity::getCustomerServiceId, Query.filterLikeEmpty(form.getCustomerServiceId(), false, true))
                .andLike(CustomerEntity::getPartnerId, Query.filterLikeEmpty(form.getPartnerId(), false, true));

        return customerManageRepository.page(form.getPageNum(), form.getPageSize(), lambdaQuery, CustomerManageConvertor::convertToInfo);
    }

    public void create(CustomerManageCreateForm form) {
        CustomerEntity entity = BeanUtil.copyProperties(form, CustomerEntity.class);
        customerManageRepository.save(entity);
    }

    public void edit(CustomerManageEditForm form) {
        CustomerEntity entity = BeanUtil.copyProperties(form, CustomerEntity.class);
        customerManageRepository.saveOrUpdate(entity);
    }

    public void remove(Integer id) {
        customerManageRepository.delete(id);
    }
}